﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace knetcat
{
    class Program
    {
        static void Main(string[] args)
        {
            //测试命令行的
            clsGetOpts getopts = new clsGetOpts();
            getopts.setArgs(args, "p:h", null);

            string strHelp = @"本软件是在2个流之间建立连接
knetcat [option] <地址> <地址>
option:
    -h 打印本帮助
    -p 本地端口
地址：
    tcp:c:hostname:port TCP协议去连接一个地址和端口
    tcp:l:port TCP协议监听
    udp:c:hostname:port UDP协议去连接一个地址和端口
    udp:l:port UDP协议监听
    open:filename 要发送的文件
    open:c:filename 接收文件
    shell   表示得到一个shell流
如果只有一个<地址>，那表示另一个流是标准输入输出流
例子：
    knetcat.exe tcp:l:8888 shell
    knetcat.exe tcp:c:localhost:8888 udp:l:9999
    knetcat.exe udp:c:localhost:9999
    如上的三个能看懂吧，端口转发加上得到一个SHELL,如上的已经经过测试，可以运行
注意：
    我这个程序的shell，最后一行是当前目录";


            string tcpc = "tcp:c:";
            string tcpl = "tcp:l:";
            string udpc = "udp:c:";
            string udpl = "udp:l:";
            string stropen = "open:";
            string openc = "open:c:";
            

            clsAsyncStream c1 = null;
            clsAsyncStream c2 = null;

            int intLocalhost = 0;



            for (int i = 0; i < getopts.optCount;i++ )
            {
                clsOptions opt = getopts.getOptions(i);
                switch (opt.name)
                {
                    case "h":
                        System.Console.WriteLine(strHelp);
                        break;
                    case "p":
                        try
                        {
                            intLocalhost = Convert.ToInt16(opt.opts);
                        }
                        catch (Exception e)
                        {
                            System.Console.WriteLine("未知：" + opt.opts+":"+e.Message);
                            Environment.Exit(0);
                            //throw;
                        }
                        break;
                    default:
                        opt.opts = opt.opts.ToLower();//都转换成小写啦
                        if (opt.opts.StartsWith(tcpc))
                        {
                            string strTmp = opt.opts.Substring(tcpc.Length);
                            int index = strTmp.IndexOf(":");//取得这个冒号分割
                            if (index>0)
                            {
                                try
                                {
                                    string strhostname = strTmp.Substring(0, index);
                                    string strPort = strTmp.Substring(index + 1);
                                    clsAsyncStreamTcpClient c = new clsAsyncStreamTcpClient();
                                    c.localPort = intLocalhost;
                                    c.hostName = strhostname;
                                    c.targetPort = Convert.ToInt16(strPort);

                                    if (c1==null)
                                    {
                                        c1 = c;
                                    }
                                    else if (c2 == null)
                                    {
                                        c2 = c;
                                    }
                                    else
                                    {
                                        System.Console.WriteLine("本程序只能在2个流之间建立连接");
                                    }

                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("未知：" + opt.opts+":"+e.Message);
                                    Environment.Exit(0);
                                    //throw;
                                }
                            }
                            else
                            {
                                System.Console.WriteLine("未知：" + opt.opts);
                                Environment.Exit(0);
                            }
                            

                        }
                        else if (opt.opts.StartsWith(tcpl))
                        {
                            string strTmp = opt.opts.Substring(tcpl.Length);
                            if (strTmp.Length > 0)
                            {
                                try
                                {

                                    clsAsyncStreamTcpListenr c = new clsAsyncStreamTcpListenr();
                                    c.localPort = Convert.ToInt16(strTmp);

                                    if (c1 == null)
                                    {
                                        c1 = c;
                                    }
                                    else if (c2 == null)
                                    {
                                        c2 = c;
                                    }
                                    else
                                    {
                                        System.Console.WriteLine("本程序只能在2个流之间建立连接");
                                    }

                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("未知：" + opt.opts + ":" + e.Message);
                                    Environment.Exit(0);
                                    //throw;
                                }
                            }
                            else
                            {
                                System.Console.WriteLine("未知：" + opt.opts);
                                Environment.Exit(0);
                            }
                            
                            
                        }
                        else if (opt.opts.StartsWith(udpc))
                        {
                            string strTmp = opt.opts.Substring(udpc.Length);
                            int index = strTmp.IndexOf(":");//取得这个冒号分割
                            if (index > 0)
                            {
                                try
                                {
                                    string strhostname = strTmp.Substring(0, index);
                                    string strPort = strTmp.Substring(index + 1);
                                    clsAsyncStreamUdpClient c = new clsAsyncStreamUdpClient();
                                    c.localPort = intLocalhost;
                                    c.hostName = strhostname;
                                    c.targetPort = Convert.ToInt16(strPort);

                                    if (c1 == null)
                                    {
                                        c1 = c;
                                    }
                                    else if (c2 == null)
                                    {
                                        c2 = c;
                                    }
                                    else
                                    {
                                        System.Console.WriteLine("本程序只能在2个流之间建立连接");
                                    }

                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("未知：" + opt.opts + ":" + e.Message);
                                    Environment.Exit(0);
                                    //throw;
                                }
                            }
                            else
                            {
                                System.Console.WriteLine("未知：" + opt.opts);
                                Environment.Exit(0);
                            }
                            

                        }
                        else if (opt.opts.StartsWith(udpl))
                        {
                            string strTmp = opt.opts.Substring(udpl.Length);
                            if (strTmp.Length > 0)
                            {
                                try
                                {

                                    clsAsyncStreamUdpListener c = new clsAsyncStreamUdpListener();
                                    c.localPort = Convert.ToInt16(strTmp);

                                    if (c1 == null)
                                    {
                                        c1 = c;
                                    }
                                    else if (c2 == null)
                                    {
                                        c2 = c;
                                    }
                                    else
                                    {
                                        System.Console.WriteLine("本程序只能在2个流之间建立连接");
                                    }

                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("未知：" + opt.opts + ":" + e.Message);
                                    Environment.Exit(0);
                                    //throw;
                                }
                            }
                            else
                            {
                                System.Console.WriteLine("未知：" + opt.opts);
                                Environment.Exit(0);
                            }

                        }
                        else if (opt.opts.StartsWith(openc))
                        {
                            string strTmp = opt.opts.Substring(openc.Length);
                            if (strTmp.Length > 0)
                            {
                                try
                                {

                                    clsAsyncStreamReceiveFile c = new clsAsyncStreamReceiveFile();
                                    c.fileName = strTmp;

                                    if (c1 == null)
                                    {
                                        c1 = c;
                                    }
                                    else if (c2 == null)
                                    {
                                        c2 = c;
                                    }
                                    else
                                    {
                                        System.Console.WriteLine("本程序只能在2个流之间建立连接");
                                    }

                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("未知：" + opt.opts + ":" + e.Message);
                                    Environment.Exit(0);
                                    //throw;
                                }
                            }
                            else
                            {
                                System.Console.WriteLine("未知：" + opt.opts);
                                Environment.Exit(0);
                            }


                        }
                        else if (opt.opts.StartsWith(stropen))
                        {
                            string strTmp = opt.opts.Substring(stropen.Length);
                            if (strTmp.Length > 0)
                            {
                                try
                                {

                                    clsAsyncStreamSendFile c = new clsAsyncStreamSendFile();
                                    c.fileName = strTmp;

                                    if (c1 == null)
                                    {
                                        c1 = c;
                                    }
                                    else if (c2 == null)
                                    {
                                        c2 = c;
                                    }
                                    else
                                    {
                                        System.Console.WriteLine("本程序只能在2个流之间建立连接");
                                    }

                                }
                                catch (Exception e)
                                {
                                    System.Console.WriteLine("未知：" + opt.opts + ":" + e.Message);
                                    Environment.Exit(0);
                                    //throw;
                                }
                            }
                            else
                            {
                                System.Console.WriteLine("未知：" + opt.opts);
                                Environment.Exit(0);
                            }


                        }
                        else if (opt.opts.StartsWith("shell"))
                        {
                            clsAsyncStreamShell c = new clsAsyncStreamShell();
                            if (c1 == null)
                            {
                                c1 = c;
                            }
                            else if (c2 == null)
                            {
                                c2 = c;
                            }
                            else
                            {
                                System.Console.WriteLine("本程序只能在2个流之间建立连接");
                            }
                            
                        }

                        else
                        {
                            System.Console.WriteLine("未知：" + opt.opts);
                            Environment.Exit(0);
                        }
                        break;

                }
            }

            //如果没有流
            if (c1==null)
            {
                System.Console.WriteLine(strHelp);
                Environment.Exit(0);
            }
            //如果第二个流为空
            if (c2==null)
            {
                c2 = new clsAsyncStreamConsole();//就设定第二个流为标准输入输出流
                
            }

            //建立连接
            c1.oppersiteStream = c2;
            c2.oppersiteStream = c1;

            //启动这两个流
            c1.start();
            c2.start();

            //死循环，因为2个流都是建立了线程
            while (true)
            {
                
            }


            
            //如下这个调试的时候才用
            //System.Console.ReadKey();

        }

        private static bool IsNum(String str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (!Char.IsNumber(str, i))
                    return false;
            }
            return true;
        }
    }
}
